package com.gxuwz.bignews.model.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.gxuwz.bignews.model.entity.NewsHistory;
import com.gxuwz.bignews.db.DatabaseHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * 新闻历史记录DAO，负责历史表的增删查改。
 */
public class NewsHistoryDao {
    private final DatabaseHelper dbHelper;

    public NewsHistoryDao(Context context) {
        dbHelper = new DatabaseHelper(context);
    }

    /**
     * 插入或更新历史记录（同一用户同一新闻多次点击时更新时间）
     */
    public void insertOrUpdateHistory(NewsHistory history) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DatabaseHelper.COLUMN_HISTORY_PHONE, history.getPhone());
        values.put(DatabaseHelper.COLUMN_HISTORY_UNIQUEKEY, history.getUniquekey());
        values.put(DatabaseHelper.COLUMN_HISTORY_TITLE, history.getTitle());
        values.put(DatabaseHelper.COLUMN_HISTORY_THUMBNAIL, history.getThumbnailPic());
        values.put(DatabaseHelper.COLUMN_HISTORY_URL, history.getUrl());
        values.put(DatabaseHelper.COLUMN_HISTORY_TIME, history.getTime());
        // UNIQUE(phone, uniquekey) ON CONFLICT REPLACE，直接insert即可实现有则更新
        db.insert(DatabaseHelper.TABLE_HISTORY, null, values);
        db.close();
    }

    /**
     * 查询某用户所有历史记录，按时间倒序排列
     */
    public List<NewsHistory> getAllHistoryByPhone(String phone) {
        List<NewsHistory> list = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(DatabaseHelper.TABLE_HISTORY, null,
                DatabaseHelper.COLUMN_HISTORY_PHONE + "=?",
                new String[]{phone}, null, null,
                DatabaseHelper.COLUMN_HISTORY_TIME + " DESC");
        while (cursor.moveToNext()) {
            NewsHistory history = new NewsHistory();
            history.setId(cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_HISTORY_ID)));
            history.setPhone(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_HISTORY_PHONE)));
            history.setUniquekey(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_HISTORY_UNIQUEKEY)));
            history.setTitle(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_HISTORY_TITLE)));
            history.setThumbnailPic(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_HISTORY_THUMBNAIL)));
            history.setUrl(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_HISTORY_URL)));
            history.setTime(cursor.getLong(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_HISTORY_TIME)));
            list.add(history);
        }
        cursor.close();
        db.close();
        return list;
    }

    /**
     * 删除某用户所有历史记录
     */
    public void deleteAllHistoryByPhone(String phone) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete(DatabaseHelper.TABLE_HISTORY, DatabaseHelper.COLUMN_HISTORY_PHONE + "=?", new String[]{phone});
        db.close();
    }

    /**
     * 删除某用户单条历史记录（根据 uniquekey）
     */
    public void deleteHistoryByPhoneAndKey(String phone, String uniquekey) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete(DatabaseHelper.TABLE_HISTORY,
                DatabaseHelper.COLUMN_HISTORY_PHONE + "=? AND " + DatabaseHelper.COLUMN_HISTORY_UNIQUEKEY + "=?",
                new String[]{phone, uniquekey});
        db.close();
    }
} 